package grape.base.service.area.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.base.service.area.po.Area;
import grape.base.service.dict.po.Dict;
import grape.common.service.common.IBaseTreeService;
import grape.common.service.trans.ITransService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2019-09-23
 */
public interface IAreaService extends IBaseTreeService<Area> , ITransService<String,String> {

    public static final String trans_areaName = "areaName";
    public static final String trans_areaParentName = "areaParentName";
    public static final String trans_areaParentNames = "areaParentNames";

    /**
     * 判断编码是否已存在
     * @param code
     * @return
     */
    default boolean existCode(String code){
        assertParamNotEmpty(code,"code 不能为空");

        Area query = new Area();
        query.setCode(code);
        int r = count(Wrappers.query(query));
        return r > 0;
    }
    default Area getByCode(String code){
        assertParamNotEmpty(code,"code 不能为空");
        Area query = new Area();
        query.setCode(code);
        return getOne(Wrappers.query(query));
    }

    @Override
    default boolean support(String type){
        return isEqualAny(type, trans_areaName, trans_areaParentName);
    }

    @Override
    default String trans(String type, String key){
        if (isEqual(type,trans_areaParentName)) {
            Area parent = getParent(key);
            if (parent != null) {
                return parent.getName();
            }
        }else if (isEqual(type,trans_areaName)) {

            Area area = getById(key);
            if (area != null) {
                return area.getName();
            }
        }else if (isEqual(type,trans_areaParentNames)) {

            List<Area> areas = getAllParents(key);
            if (!isEmpty(areas)) {
                return areas.stream().map(item -> item.getName()).collect(Collectors.joining(" "));
            }
        }
        return null;
    }
}
